package com.study.ioc.demo.beanpostprocessor;

import com.study.ioc.demo.aware.MyAware;
import com.study.ioc.demo.bean.TestBeanOne;
import com.study.ioc.demo.bean.TestBeanTwo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * 在bean初始化的时候，也就是Spring激活bean的init-method方法的前后，会调用BeanPostProcessor的postProcessBeforeInitialization方法和postProcessAfterInitialization。
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    /**
     * 在调用显示的初始化之前完成一些定制的初始化任务
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //System.out.println("postProcessBeforeInitialization..."+beanName+"..."+bean);
        if (bean.getClass().getName().equals(TestBeanOne.class.getName())) {
            return bean;
        }
        if (bean.getClass().getName().equals(TestBeanTwo.class.getName())) {
            return bean;
        }
        if (bean.getClass().getName().equals(MyAware.class.getName())) {
            return bean;
        }
        return bean;
    }

    /**
     * 在调用显示的初始化之后完成一些定制的初始化任务
     *
     * @param bean
     * @param beanName
     * @return
     * @throws BeansException
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        //System.out.println("postProcessAfterInitialization..."+beanName+"..."+bean);
        if (bean.getClass().getName().equals(TestBeanOne.class.getName())) {
            return bean;
        }
        return bean;
    }

}